<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

</body>
<script>
// (1)学习try-catch基础语法
//json字符串
//用途:用于网络传输的字符串
//特征:
// let str = '{"username": "zs", "age": 30}'
// try-catch捕获错误,让我自己处理错误,不让浏览器处理
/*
try{
  let str = '{username: "zs", "age": 30}'
  //产生错误,方式1-通过JSON.parse产生错误
  // let obj = JSON.parse(str)
  //产生错误,方式2-通过throw错误
  throw new Error('产生错误')
}catch(e){
  console.log(e)
}
console.log(123)
*/

//(2)在promise当中产生错误
/**/
let p = new Promise((resolve,reject)=>{
  //方式1-通过reject方法
  // reject({status:400, message: '失败了'})
  //方式2-通过throw
  // throw new Error('产生错误')
  //方式3-通过resolve一个失败的promise
  // resolve(new Promise((resolve2, reject2)=> reject2('产生错误')))
  resolve(Promise.reject('产生错误'))
})
p.then((data)=>{
  console.log(data)
}).catch((err)=>{
  console.log('err', err)
})

/*
async function fn(){
  try{
    let data = await new Promise((resolve,reject)=>{
      // resolve(123)
      reject({status:400, message: '失败了'})
    })
    console.log(data)
  }catch(err){
    console.log(err)
  }
  console.log(123)
}
fn()
*/
</script>
</html>
